/*
 * smart-pwm.h
 *
 * The smart pwm driver for SinhMicro's SINH51 platform.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */

#ifndef __SMART_PWM_H__
#define __SMART_PWM_H__

#include "hal-config.h"
#include "types.h"

/*
 * smart pwm micros list below, and x can be 0, 2.
 * SPWMX_TIMER_INIT
 * init the smartpwm timer lowlayer 
 * 
 * SPWMX_TIMER_START
 * start the smartpwm timer lowlayer
 */

#include "smart-pwm/smart-pwm-ss805x.h"          

/* 
 * smart pwm functions list below, and x can be 0, 2.

 * 1. spwm_init(spwm)
 * This function is used to init the smart pwmx.
 * @param[in] spwm  0 for smartpwm0, 1 for smartpwm1, etc
 * @return None  
 * Note: 
 * 1.Since smart pwmx runs on timerx, users must call the tx_init().
 * 
 * 2. spwm_cc_enable(spwm, cc)
 * This function is used to enable the ccx of smart pwmx.
 * @param[in] spwm  0 for smartpwm0, 1 for smartpwm1, etc
 * @param[in] cc    0 for cc0, 1 for cc1, etc
 * @return None
 * 
 * 3. spwm_cc_disable(spwm, cc)
 * This function is used to disable the ccx of smart pwmx.
 * @param[in] spwm  0 for smartpwm0, 1 for smartpwm1, etc
 * @param[in] cc    0 for cc0, 1 for cc1, etc
 * @return None
 * 
 * 4. spwm_cc_io_switch(spwm, cc, io)
 * This function is used to switch the smart pwmx(ccx) io.
 * @param[in] spwm  0 for smartpwm0, 1 for smartpwm1, etc
 * @param[in] cc    0 for cc0, 1 for cc1, etc
 * @param[in] io    0x00 for p00, 0x01 for p01, etc
 * @return None
 * Note: this function works when CONFIG_SMPWMX_CCX_SEL_IO enabled.
 * 
 * 5. spwm_set_freq_by_reload_val(spwm, reload val)
 * This function is used to change the smart pwmx freq by timer reload value.
 * @param[in] spwm          0 for smartpwm0, 1 for smartpwm1, etc
 * @param[in] reload val    user can call TX_OF_RATIO_TO_RL_VAL() micro get
 * the timer reload value by specify the timer overflow ratio.
 * @return None
 * Note: 
 * 1.All ccx of the same smartpwm have the same frequency, equal to the timerx
 * overflow ratio.
 * 2.When frequency changes, the ccx duty also changes, user needs to handle 
 * the situation by himself.
 * 3.User should enable CONFIG_TIMERx_OF_RATIO_CHG if smartpwm freq may change.
 *                         
 * 6. spwm_set_duty_relative_max_val(spwm, cc, duty)
 * This function is used to set the ccx duty.
 * @param[in] spwm  0 for smartpwm0, 1 for smartpwm1, etc
 * @param[in] cc    cc select, 0 for cc0, 1 for cc1.
 * @param[in] duty  0 ~ max duty(please get from smart_pwm_get_max_duty().
 * @return None 
 * The real duty equal to "duty * 100 / smart_pwm_get_max_duty()".
 *     
 * 7. spwm_get_max_duty(spwm)
 * This function is used to get the max smart pwmx duty.
 * @param[in] spwm  0 for smartpwm0, 1 for smartpwm1, etc
 * @return max duty
 * 
 */
#if (CONFIG_USING_SMPWM0 == 1 || CONFIG_USING_SMPWM2 == 1)
    void spwm_init(uint8_t spwm);

    #if (CONFIG_USING_SMPWM0_CC0 == 1 || \
        CONFIG_USING_SMPWM2_CC0 == 1 || CONFIG_USING_SMPWM2_CC1 == 1 ||  \
        CONFIG_USING_SMPWM2_CC2 == 1 || CONFIG_USING_SMPWM2_CC3 == 1 ||  \
        CONFIG_USING_SMPWM2_CC4 == 1)
        void spwm_cc_enable(uint8_t spwm, uint8_t cc);
        void spwm_cc_disable(uint8_t spwm, uint8_t cc);
        void spwm_set_duty_relative_max_val(uint8_t spwm, uint8_t cc, uint16_t duty);
    #else
        #define spwm_cc_enable(spwm, cc)
        #define spwm_cc_disable(spwm, cc)
        #define spwm_set_duty_relative_max_val(spwm, cc, duty)
    #endif

    void spwm_set_freq_by_reload_val(uint8_t spwm, uint16_t reload_val);
    uint16_t spwm_get_max_duty(uint8_t spwm);
    void t2_set_duty(uint8_t cc, uint16_t duty);
#else
    #define spwm_init(spwm)
    #define spwm_cc_enable(spwm, cc)
    #define spwm_cc_disable(spwm, cc)
    #define spwm_set_duty_relative_max_val(spwm, cc, duty)
    #define spwm_set_freq_by_reload_val(spwm, reload_val)
    #define spwm_get_max_duty(spwm) (0)
#endif

#endif
